package com.railway.switchs.action.business;

import com.railway.switchs.aspect.OperationLog;
import com.railway.switchs.base.BaseResponse;
import com.railway.switchs.domain.dto.business.RepairRecord;
import com.railway.switchs.domain.query.business.RepairRecordQuery;
import com.railway.switchs.enums.EnumReturnResult;
import com.railway.switchs.service.business.IRepairRecordService;
import com.railway.switchs.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName RepairRecordAction
 * @Author 魏梓晟
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/repairRecord")
public class RepairRecordAction {

    @Autowired
    private IRepairRecordService repairRecordService;

    /**
     * 进入维修记录列表页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return "repairRecord/list";
    }


    /**
     * 获取维修记录数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public RepairRecordQuery getList(RepairRecordQuery query) {
        try {
            query = repairRecordService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取维修记录列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取维修记录数据异常");
        }
        return query;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView preAdd(long repairId){
        ModelAndView mav = new ModelAndView();
        mav.addObject("repairId",repairId);
        mav.addObject("userAccount", UserUtil.getUserAccount());
        mav.setViewName("repairRecord/add");
        return mav;
    }

    /**
     * 新增检修记录
     * @param repairRecord
     * @param result
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="7",actionType ="0" )
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated RepairRecord repairRecord, BindingResult result) {
        BaseResponse response = new BaseResponse();
        try {
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("RepairRecord-add参数有误");
                log.info("RepairRecord-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            repairRecordService.add(repairRecord);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("新增检修记录成功");
        } catch (Exception e) {
            log.error("新增检修记录异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("新增检修记录失败");
        }
        return response;
    }

}
